package org.lmd.platform.sys.iml;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.lmd.platform.constant.Constant;
import org.lmd.platform.dao.sys.UserMapper;
import org.lmd.platform.entity.sys.OrgEntity;
import org.lmd.platform.entity.sys.RoleEntity;
import org.lmd.platform.sys.OrgService;
import org.lmd.platform.sys.RoleService;
import org.lmd.platform.sys.UserService;
import org.lmd.platform.util.Query;
import org.lmd.platform.vo.PageUtils;
import org.lmd.platform.dao.sys.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class RoleServiceIml extends ServiceImpl<RoleMapper,RoleEntity> implements RoleService {

    @Autowired
    private OrgService orgService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String roleName = (String)params.get("roleName");

        Page<RoleEntity> page = this.selectPage(
                new Query<RoleEntity>(params).getPage(),
                new EntityWrapper<RoleEntity>().like(StringUtils.isNotBlank(roleName),"role_name", roleName)
                        .addFilterIfNeed(params.get(Constant.SQL_FILTER) != null, (String)params.get(Constant.SQL_FILTER))
        );

        for(RoleEntity sysRoleEntity : page.getRecords()){
            OrgEntity orgEntity = orgService.selectById(sysRoleEntity.getDeptId());
            if(orgEntity != null){
                sysRoleEntity.setDeptName(orgEntity.getName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(RoleEntity role) {
        role.setCreateTime(new Date());
        this.baseMapper.saveRole(role);
        this.saveRoleDeptList(role.getRoleId(),role.getDeptIdList());
        this.saveRoleMenuList(role.getRoleId(),role.getMenuIdList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleMenuList(Long roleId, List<Long> menuIdList) {
        //先删除角色与菜单关系
        this.deleteRoleMenuBatch(new Long[]{roleId});
        if(menuIdList.size() == 0){
            return ;
        }

        //保存角色与菜单关系
        List<Map<String,Long>> list = new ArrayList<>(menuIdList.size());
        for(Long menuId : menuIdList){
            Map<String,Long> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("menuId",menuId);
            list.add(map);
        }
        this.baseMapper.saveOrUpdateRoleMenus(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRoleDeptList(Long roleId, List<Long> deptIdList) {
        //先删除角色与部门关系
        this.deleteRoleDeptBatch(new Long[]{roleId});
        if(deptIdList.size() == 0){
            return ;
        }
        //保存角色与菜单关系
        List<Map<String,Long>>  list = new ArrayList<>(deptIdList.size());
        for(Long deptId : deptIdList){
            Map<String,Long> map=new HashMap<>();
            map.put("roleId",roleId);
            map.put("deptId",deptId);
            list.add(map);
        }
        this.baseMapper.saveOrUpdateRoleDepts(list);
    }
    @Override
    public void update(RoleEntity role) {
        this.updateAllColumnById(role);
        this.saveRoleDeptList(role.getRoleId(),role.getDeptIdList());
        this.saveRoleMenuList(role.getRoleId(),role.getMenuIdList());
    }

    @Override
    public void deleteBatch(Long[] roleIds) {
        this.baseMapper.deleteRoleBatch(roleIds);
        this.baseMapper.deleteRoleMenusBatch(roleIds);
        this.baseMapper.deleteRoleDeptBatch(roleIds);
        this.userMapper.deleteUserRoleBatch(roleIds);
    }

    @Override
    public List<Long> queryRoleMenuIdList(Long roleId) {
        return this.baseMapper.queryRoleMenuIdList(roleId);
    }

    @Override
    public List<Long> queryRoleDeptIdList(Long[] roleIds) {
        return this.baseMapper.queryRoleDeptIdList(roleIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleDeptBatch(Long[] roleIds) {
        this.baseMapper.deleteRoleDeptBatch(roleIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleMenuBatch(Long[] roleIds) {
        this.baseMapper.deleteRoleMenusBatch(roleIds);
    }

}
